package com.fnzn.entranceGuard.rest.usrCloud;

import com.alibaba.fastjson.JSONObject;
import com.fnzn.entranceGuard.service.AuthGuardService;
import com.fnzn.entranceGuard.util.SpringUtil;
import com.github.pig.common.constant.CommonConstant;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author wf_sxf@163.com
 * @date 2018-07-10 9:40
 */
@Component
public class UsrApi {



    public static String user;

    public static String pass;


    @Value("${usr.username}")
    public void setUser(String user) {
        UsrApi.user = user;
    }

    @Value("${usr.password}")
    public void setPass(String pass) {
        UsrApi.pass = pass;
    }

    private static String USR_TOKEN="USR_TOKEN";

    private static String login ="https://cloudapi.usr.cn/usrCloud/user/login";
    private static String getDevice ="https://cloudapi.usr.cn/usrCloud/dev/getDevice";


    public static String login(){
        RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate",RedisTemplate.class);

        String p =SecureUtil.md5().digestHex(pass);
        String content ="{" +
                "    \"account\": \""+user+"\"," +
                "    \"password\": \""+p+"\"" +
                "}";

        String result = HttpUtil.post(login,content );

        System.out.println(result);
        com.xiaoleilu.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(result);
        com.xiaoleilu.hutool.json.JSONObject json = JSONUtil.parseObj( jsonObject.get("data"));
        //有效期两小时
        redisTemplate.opsForValue().set(USR_TOKEN, json.get("token"),7100, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get(USR_TOKEN));
        return result;
    }

    /**
     *
     * @param deviceId
     * @return 在线 true  离线 false
     */
    public static boolean getDevice(String deviceId){
        try{

            String result = getResult(deviceId);
            com.xiaoleilu.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(result);
            Integer status = (Integer) jsonObject.get("status");
            if(status!=0){
                login();
                jsonObject = JSONUtil.parseObj(getResult(deviceId));
            }
            com.xiaoleilu.hutool.json.JSONObject json = JSONUtil.parseObj( jsonObject.get("data"));

            com.xiaoleilu.hutool.json.JSONObject j = JSONUtil.parseObj(json.get("device"));
            Object onlineStatus = j.get("onlineStatus");
            //在线
            if(onlineStatus!=null&&onlineStatus.equals(1)){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static String getResult(String deviceId){
        RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate",RedisTemplate.class);

        Object token = redisTemplate.opsForValue().get(USR_TOKEN);
        if(token==null){
            login();
        }
        String content ="{" +
                "    \"deviceId\": \""+deviceId+"\"," +
                "    \"token\": \""+token+"\"" +
                "}";

        return HttpUtil.post(getDevice,content);
    }
    public static void main(String[] args) {
        System.out.println(getDevice("00012950000000000001"));
    }

}
